// main/FarmSystemDemo.java
package org.example.main;

import org.example.chain.SafetyData;
import org.example.facade.FarmSystemFacade;
import org.example.observer.EnvironmentData;
import org.example.model.FarmMachine;
import org.example.strategy.ObstacleAvoidanceStrategy;
import org.example.strategy.WetlandPathStrategy;
import org.example.command.ChangePathCommand;

import java.util.Scanner;

public class FarmSystemDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("===== 智能无人驾驶农机系统演示 =====");

        // 使用外观模式简化系统操作
        FarmSystemFacade systemFacade = new FarmSystemFacade();

        // 1. 创建农机设备
        FarmMachine seeder = systemFacade.createFarmMachine("播种机", "SD-001");
        FarmMachine harvester = systemFacade.createFarmMachine("收割机", "HG-002");

        while (true) {
            System.out.println("\n请选择操作：");
            System.out.println("1. 为农机分配任务");
            System.out.println("2. 为农机规划路径");
            System.out.println("3. 更新环境数据（正常）");
            System.out.println("4. 更新环境数据（湿地）");
            System.out.println("5. 更新环境数据（有障碍物）");
            System.out.println("6. 模拟故障");
            System.out.println("7. 更改路径规划策略（避障）");
            System.out.println("8. 更改路径规划策略（湿地）");
            System.out.println("9. 撤销上一个命令");
            System.out.println("0. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    System.out.println("请选择农机（1: 播种机，2: 收割机）：");
                    int machineChoice1 = scanner.nextInt();
                    scanner.nextLine();
                    FarmMachine machine1 = (machineChoice1 == 1) ? seeder : harvester;
                    systemFacade.assignTaskToMachine(machine1);
                    break;
                case 2:
                    System.out.println("请选择农机（1: 播种机，2: 收割机）：");
                    int machineChoice2 = scanner.nextInt();
                    scanner.nextLine();
                    FarmMachine machine2 = (machineChoice2 == 1) ? seeder : harvester;
                    systemFacade.planPathForMachine(machine2);
                    break;
                case 3:
                    EnvironmentData normalData = new EnvironmentData();
                    normalData.setSoilMoisture(50.0);
                    normalData.setTemperature(25.0);
                    normalData.setCropStatus("生长良好");
                    systemFacade.updateEnvironmentData(normalData);
                    break;
                case 4:
                    EnvironmentData wetlandData = new EnvironmentData();
                    wetlandData.setSoilMoisture(80.0);
                    wetlandData.setTemperature(24.0);
                    wetlandData.setCropStatus("生长良好");
                    systemFacade.updateEnvironmentData(wetlandData);
                    break;
                case 5:
                    EnvironmentData obstacleData = new EnvironmentData();
                    obstacleData.setSoilMoisture(55.0);
                    obstacleData.setTemperature(26.0);
                    obstacleData.setCropStatus("生长良好");
                    obstacleData.setHasObstacle(true);
                    obstacleData.setObstacleDistance(3.5);
                    systemFacade.updateEnvironmentData(obstacleData);
                    break;
                case 6:
                    System.out.println("请选择农机（1: 播种机，2: 收割机）：");
                    int machineChoice3 = scanner.nextInt();
                    scanner.nextLine();
                    FarmMachine machine3 = (machineChoice3 == 1) ? seeder : harvester;
                    SafetyData faultData = new SafetyData();
                    faultData.setFaultInfo("发动机异常");
                    faultData.setFaultLevel(4);
                    systemFacade.handleSafetyEvent(machine3, "检测到故障", faultData);
                    break;
                case 7:
                    System.out.println("请选择农机（1: 播种机，2: 收割机）：");
                    int machineChoice4 = scanner.nextInt();
                    scanner.nextLine();
                    FarmMachine machine4 = (machineChoice4 == 1) ? seeder : harvester;
                    ChangePathCommand obstacleCommand = new ChangePathCommand(machine4, machine4.getPathPlanningContext(), new ObstacleAvoidanceStrategy());
                    systemFacade.getCommandInvoker().executeCommand(obstacleCommand);
                    break;
                case 8:
                    System.out.println("请选择农机（1: 播种机，2: 收割机）：");
                    int machineChoice5 = scanner.nextInt();
                    scanner.nextLine();
                    FarmMachine machine5 = (machineChoice5 == 1) ? seeder : harvester;
                    ChangePathCommand wetlandCommand = new ChangePathCommand(machine5, machine5.getPathPlanningContext(), new WetlandPathStrategy());
                    systemFacade.getCommandInvoker().executeCommand(wetlandCommand);
                    break;
                case 9:
                    systemFacade.undoLastCommand();
                    break;
                case 0:
                    System.out.println("\n===== 智能无人驾驶农机系统演示结束 =====");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }
}